package javajavado.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javajavado.negocio.GestionIdea;

@Controller
public class IdeaEstadoController {
	
	
	@RequestMapping(value = "/listaIdea")	
	protected ModelAndView login(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		GestionIdea neg = new GestionIdea();
		System.out.println("carga");
		System.out.println(neg.listar2().size());
		return new ModelAndView("lstIdeasBorrador", "ideas", neg.listar2());
	}
	
	@RequestMapping(value = "/buscaidea")	
	protected ModelAndView busidea(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		int id = Integer.parseInt(request.getParameter("id"));
		String nom = request.getParameter("nom").toString();
		GestionIdea neg = new GestionIdea();
		request.setAttribute("nombre", nom);
		return new ModelAndView("modIdea", "id", id);
	}
	
	@RequestMapping(value = "/grabarIdea")	
	protected ModelAndView graba(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		int id = Integer.parseInt(request.getParameter("hdidea"));
		String rd = request.getParameter("rdestado").toString();
		GestionIdea neg = new GestionIdea();
		String Error = "";
		ModelAndView ventana = null;
		if(neg.graba(id, rd)==0){
			String nom = request.getParameter("hdnombre").toString();
			request.setAttribute("nombre", nom);
			Error = "Error no se pudo actualizar";
			request.setAttribute("error", Error);
			ventana = new ModelAndView("modIdea", "id", id);
		}else{
			ventana = new ModelAndView("lstIdeasBorrador", "ideas", neg.listar2());
		}
		return ventana;
	}

} 
